﻿using System;
using System.Web.Mvc;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Models;
using Kaiser.Models.JsonModels;
using Kaiser.Extensions;
using System.Web.Script.Serialization;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Services;
using Kaiser.Modules.Cubic.ViewModel;

namespace Kaiser.Modules.Cubic.Controllers
{

    public class ProductManageController : KaiserBaseController
    {
        #region 变量和属性
        private readonly IProductService _productService;

        public ProductManageController(IProductService productService)
        {
            this._productService = productService;
        }


        #endregion

        public ActionResult ProductList()
        {
            return View();
        }

        /// <summary>
        /// 产品列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductList(ProductSearch search, PagingInfo pageinfo, FormCollection form)
        {
            search.orderStr = pageinfo.Sort;
            search.sortDirection = pageinfo.Order;
            var result = _productService.SearchProduct(search, pageinfo);

            return Json(result);
        }
 

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddProduct(ProductDetail model)
        {
            var result = new JsonReturnMessages();

            try
            {

                model.CreateDateTime = DateTime.Now;
                model.CreateUserFullName = User.Identity.Name;
                model.CreateUserName = CurrentUser.Name;
                model.LastModifyDateTime = DateTime.Now;
                model.LastModifyUserFullName = User.Identity.Name;
                model.LastModifyUserName = CurrentUser.Name;

                _productService.Insert(model);

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }


        public ActionResult AddProduct(string CategoryId)
        {
            var model = new ProductDetail();
            model.ProductId = Guid.NewGuid().GenerateGuid().ToString();
            model.CategoryId = CategoryId;
            return View("EditProduct", model);
        }

        public ActionResult EditProduct(string id)
        {
            DomainModel.Product.ProductDetail model = _productService.Get(id);
            model.DehumidifierDescription = Server.UrlDecode(model.DehumidifierDescription);
            model.EnglishDehumidifierDescription = Server.UrlDecode(model.EnglishDehumidifierDescription);
            model.Features = Server.UrlDecode(model.Features);
            return View(model);
        }


      
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditProduct(string id, ProductDetail model, FormCollection form)
        {
            var result = new JsonReturnMessages();

            try
            {
                ProductDetail oldSort = _productService.Get(id);
                model.CreateDateTime = oldSort.CreateDateTime;
                model.CreateUserFullName = oldSort.CreateUserFullName;
                model.CreateUserName = oldSort.CreateUserName;
                model.LastModifyDateTime = DateTime.Now;
                model.LastModifyUserFullName = User.Identity.Name;
                model.LastModifyUserName = CurrentUser.Name;
                _productService.Update(model);
             
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 批量删除产品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelProduct(string ids)
        {
            var result = new JsonReturnMessages();
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {

                    _productService.Delete(ids);
                    result.success = true;
                    result.msg = "删除成功！";

                }
                catch (Exception ex)
                {
                    result.success = false;
                    result.msg = ex.Message;

                }
            }
            else
            {
                result.success = false;
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        public ActionResult ProductDetail(string id)
        {
            ProductDetail oldSort = _productService.Get(id);
            return View(oldSort);
        }

        #region 产品图片管理

        public ActionResult ProductPhotoList()
        {

            return View();
        }

        [HttpPost]
        public ActionResult ProductPhotoList(string productId,  PagingInfo pageinfo)
        {

            var result = _productService.SearchProductPhoto(productId,pageinfo);

            return Json(result);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddProductPhoto(Photo model)
        {
            var result = new JsonReturnMessages();

            try
            {
                _productService.InsertProductPhoto(model);

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }


        public ActionResult AddProductPhoto()
        {
            return View("EditProductPhoto",new Photo());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditProductPhoto(Photo model)
        {
            var result = new JsonReturnMessages();

            try
            {
                _productService.UpdateProductPhoto(model);

                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }


        public ActionResult EditProductPhoto(string id)
        {
           var model= _productService.GetProductPhoto(id);
           return View("EditProductPhoto", model);
        }


        public ActionResult DelProductPhoto(string ids)
        {
            var result = new JsonReturnMessages();
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {

                    _productService.DelProductPhoto(ids);
                    result.success = true;
                    result.msg = "删除成功！";

                }
                catch (Exception ex)
                {
                    result.success = false;
                    result.msg = ex.Message;

                }
            }
            else
            {
                result.success = false;
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        #endregion
    }
}
